package com.neusoft.ysu.populationmanagement.service.impl;

import com.neusoft.ysu.populationmanagement.constant.Errors;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.FloPop;
import com.neusoft.ysu.populationmanagement.entity.OperationLog;
import com.neusoft.ysu.populationmanagement.entity.Population;
import com.neusoft.ysu.populationmanagement.mapper.VFloPopMapper;
import com.neusoft.ysu.populationmanagement.vo.VFloPop;
import com.neusoft.ysu.populationmanagement.service.IVFloPopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2024-06-18
 */
@Service
public class VFloPopServiceImpl implements IVFloPopService {
    @Autowired
    FloPopServiceImpl floPopService;
    @Autowired
    OperationLogServiceImpl operationLogService;
    @Autowired
    PopulationServiceImpl populationService;
    @Autowired
    VFloPopMapper vFloPopMapper;
    @Override
    public CommonResult insertVFLo(VFloPop vFloPop, OperationLog log) {
        vFloPop.setId(null);
        vFloPop.setIsDelete(TabelDic.IS_DELETE_NO);
        Population population = new Population(vFloPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回201号错误
            return new CommonResult<>(Errors.ERROR_FLO_INSERT, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vFloPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回201号错误
            return new CommonResult<>(Errors.ERROR_FLO_INSERT, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vFloPop.setId(population.getId());
        FloPop floPop = new FloPop(vFloPop);
        CommonResult flo_result = floPopService.insertFloPop(floPop);
        if (flo_result.getCode() == 0) {
            flo_result.setCode(Errors.ERROR_FLO_INSERT);
            return flo_result;
        } else
            return new CommonResult<>(1, "流动人口信息插入成功", 1);
    }

    /**
     * @param vFloPop
     * @param log
     * @return CommonResult 更新flo_pop
     * @author zzj
     * @date 2024-06-20 09:59
     */
    @Override
    public CommonResult updateVFLo(VFloPop vFloPop, OperationLog log) {
        vFloPop.setId(null);
        Population population = new Population(vFloPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回202号错误
            return new CommonResult<>(Errors.ERROR_FLO_UPDATE, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vFloPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回202号错误
            return new CommonResult<>(Errors.ERROR_FLO_UPDATE, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vFloPop.setId(population.getId());
        FloPop floPop = new FloPop(vFloPop);
        CommonResult result = floPopService.updateFloPop(floPop);
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_FLO_UPDATE);
            return result;
        } else
            return new CommonResult<>(1, "流动人口信息变更成功", 1);
    }

    @Override
    public CommonResult deleteVFLo(VFloPop vFloPop, OperationLog log) {
        vFloPop.setId(null);
        Population population = new Population(vFloPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回203号错误
            return new CommonResult<>(Errors.ERROR_FLO_DELETE, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vFloPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回203号错误
            return new CommonResult<>(Errors.ERROR_FLO_DELETE, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vFloPop.setId(population.getId());
        FloPop floPop = new FloPop(vFloPop);
        CommonResult result = floPopService.deleteFlo(floPop);
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_FLO_DELETE);
            return result;
        } else
            return new CommonResult<>(1, result.getMsg(), 1);
    }

    @Override
    public CommonResult selectFloByIdNum(String idnum) {
        List<VFloPop> floPopList=vFloPopMapper.selectByIdNum(idnum);
        if(floPopList.size()>1){
            System.out.println("error:Vflo表有重复数据");
            return new CommonResult<>(Errors.ERROR_FLO_SELECT,"Vflo表有重复数据",null);
        }
        if(floPopList.size()==1){
            return new CommonResult<>(1,"查询成功",floPopList.get(0));
        }
        else if(floPopList.size()==0){
            return new CommonResult<>(Errors.ERROR_FLO_SELECT,"未找到对应人员",null);
        }
        return null;
    }
}
